﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using NetCMS.Content.Collect;
using System.Data;
using System.Configuration;

namespace NetCMS.Content
{

    public class MyTimer
    {
        private static readonly MyTimer _ScheduledTask = null;
        public Timer UpdateTimer = null;
        private int Interval = 1000*60;//1000=1秒钟
        private string doTime = "09:00:00";//执行时间
        private int _IsRunning;        

        static MyTimer()
        {
            _ScheduledTask = new MyTimer();
        }
        public static MyTimer Instance()
        {
            return _ScheduledTask;
        }
        public void Start()
        {            

            if (UpdateTimer == null)
            {
                string inter = ConfigurationManager.AppSettings["cjTimeInterval"];
                if (inter != null && inter != "")
                {
                    Interval = int.Parse(ConfigurationManager.AppSettings["cjTimeInterval"]);
                }
                else
                {
                    NetCMS.Content.Common.FsLog.CollectLogs(1, 0, "读取web.config的cjTimeInterval错误", "");
                }
                UpdateTimer = new Timer(new TimerCallback(UpdateTimerCallback), null, Interval, Interval);

                NetCMS.Content.Common.FsLog.CollectLogs(1, 0, "定时器启动，采集间隔为"+Interval, "");//启动时候执行一次
                int cjcount = NetCMS.Content.Collect.Collect.GetAutoCollectRules(); //1、如果有cjstate=1的规则，则加入cjqueue                
                NetCMS.Content.Collect.Collect.myAutoCollect();//自动采集并入库    

            }

        }

        private void UpdateTimerCallback(object sender)
        {              
            if (Interlocked.Exchange(ref _IsRunning, 1) == 0)
            {
                //按照顺序执行的，
                try
                {
                    //NetCMS.Content.Common.FsLog.CollectLogs(1, 0, "定时器执行", "内容");
                  
                    DateTime temp = DateTime.Now;
                    if (temp.Minute ==0 && temp.Second==0)
                    {
                        int cjcount = NetCMS.Content.Collect.Collect.GetAutoCollectRules(); //1、如果有cjstate=1的规则，则加入cjqueue                
                        NetCMS.Content.Collect.Collect.myAutoCollect();//自动采集并入库                              
                    }




                    //int cjcount = NetCMS.Content.Collect.Collect.GetAutoCollectRules(); //1、如果有cjstate=1的规则，则加入cjqueue                
                    //NetCMS.Content.Collect.Collect.myAutoCollect();//自动采集并入库                              
                    
                }
                catch (Exception ex)
                {
                    NetCMS.Content.Common.FsLog.CollectLogs(1, 0, "定时器他妈的又异常", ex.Message);
                }
                finally
                {
                    Interlocked.Exchange(ref _IsRunning, 0);
                }
            }
        }
        public void Stop()
        {
            if (UpdateTimer != null)
            {
                UpdateTimer.Dispose();
                UpdateTimer = null;
                NetCMS.Content.Common.FsLog.CollectLogs(1, 0, "定时器关闭...", "");
            }
        }
    }


}
